/**
    GpsNetwork lets you download files and log some system parameters from NetRs DGPS receivers.

    Copyright (C) 2009  Eduardo Maureira

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    email:emaureir@gmail.com
 */
package process;

import java.util.ArrayList;
import java.util.Stack;
import org.apache.log4j.Logger;

/**
 * @author eduardo
 *
 */
public class TaskList extends Thread {

	Stack<SystemCall> taskList;
	SystemCall currentTask;
	boolean detener;
	private ArrayList<IEventoTaskListListener> listeners;

	private static final Logger logger = Logger.getLogger(TaskList.class);

	public TaskList() {
		super();
		logger.info("Nueva Tasklist");
		currentTask = null;
		this.taskList = new Stack<SystemCall>();
		detener = false;
		this.listeners = new ArrayList<IEventoTaskListListener>();
	}

	public boolean apilar  (SystemCall sysCall)
	{
		if (sysCall==null)
		{
			return false;
		}
		return this.taskList.add(sysCall);
	}

	/**
	 * 
	 * @param tipo
	 * @param count cantidad de tareas
	 */
	private void enviaEventoTaskList(short tipo)
	{
		EventoTaskList evt;
		evt = new EventoTaskList (this,tipo,taskList.size());
		dispatchMyEvent(evt);
	}

	//	public Stack<SystemCall> getTaskList() {
	//		return taskList;
	//	}

	public boolean isDetener() {
		return detener;
	}

	public void setDetener(boolean detener) {
		this.detener = detener;
	}

	public void addMyEventListener( IEventoTaskListListener mel ) {
		listeners.add( mel );  
	}

	private void dispatchMyEvent(EventoTaskList me) 
	{ // maybe needs some args?
		for ( int j = 0; j < listeners.size(); j++ ) {
			IEventoTaskListListener mel = (IEventoTaskListListener) listeners.get(j);
			if ( mel != null ) {

				mel.handleTaskListEvent( me );
			}
		}		  
	} // end dispatchMyEvent();


	@Override
	public void run() {
		logger.info("Ejecutando Tasklist");
		enviaEventoTaskList(EventoTaskList.STARTRUN);
		while ((!detener) && (taskList.size()>0))
		{
			currentTask = taskList.pop();
			currentTask.start();
			while (currentTask.isAlive())
			{
				System.out.print(".");
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (detener)
		{
			logger.info("Destruyendo procesos en cola");
			currentTask.getProceso().destroy();
		}
		logger.info("Finalizando Tasklist");
		enviaEventoTaskList(EventoTaskList.ENDRUN);
	}



	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
